package com.jin.webvideo.externalInterfaces.spi.videosearch;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jin.webvideo.entity.Page;
import com.jin.webvideo.externalInterfaces.video.YZZYApi;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.base.spi.InterfaceProvider;
import com.jin.webvideo.base.spi.anno.Register;
import com.jin.webvideo.util.sys.ClientUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 优质资源网接口
 */
@Register(name = "YZZY")
public class YzzySearchInterface extends SearchInterface {
    private static final String url = "https://api.1080zyku.com/inc/api_mac10.php?ac=list&pg=%s&ps=%s&wd=%s";
    @Autowired
    private YZZYApi api;

    private int total = 0;
    public YzzySearchInterface(InterfaceProvider provider) {
        super(provider);
    }

    @Override
    protected List<Filmography> search(String key, String[] tags, int pageNo, int pageSize) throws Exception {
        String data = ClientUtil.httpGet(String.format(url, pageNo, pageSize, key));
        JSONObject json = JSONObject.parseObject(data);
        total = json.getInteger("total");
        int size = json.getInteger("limit");
        Page page = Page.createPage(total, size, Integer.valueOf(pageNo));
        JSONArray list = json.getJSONArray("list");
        String docid = "";
        for (int i = 0, l = list.size(); i < l; i++) {
            String id = list.getJSONObject(i).getString("vod_id");
            docid = docid + id + ",";
        }
        if (docid.length() > 0) {
            docid = docid.substring(0, docid.length() - 1);
        }
        return api.detail(docid);
    }

    @Override
    protected int total(String key, String[] tags) throws Exception {
        int total = this.total;
        this.total = -1;
        return total;
    }
}
